<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button type="primary" size="small" icon="el-icon-plus" @click="dialogFormVisible=true">新增角色</el-button>

            <el-table
              :data="tableData"
              style="width: 100%;margin: 20px 0"
              border
            >
              <el-table-column
                type="index"
                label="序号"
                width="150px"
              />
              <el-table-column
                prop="name"
                label="角色名"
                sortable
                width="150px"
              />
              <el-table-column
                prop="description"
                label="描述"
                sortable
              />
              <el-table-column
                label="操作"
                width="250px"
                align="center"
              >
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="assignPermissions(scope.row)">分配权限</el-button>
                  <el-button type="text" size="small" @click="setRoles(scope.row)">修改</el-button>
                  <el-button type="text" size="small" @click="deleteRoles(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-pagination
              :current-page="query.page"
              :page-sizes="[2, 4, 6, 8]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-row>
              <el-col :span="8" :offset="3">
                <el-form ref="form" label-width="80px">
                  <el-form-item label="企业名称">
                    <el-input disabled :value="companyInfo.name" />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input disabled :value="companyInfo.companyAddress" />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input disabled :value="companyInfo.companyPhone" />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input disabled :value="companyInfo.mailbox" />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input disabled :value="companyInfo.remarks" />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <!-- 分配权限 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogRightVisible"
      width="40%"
    >
      <el-tree
        ref="treeRights"
        :props="{label:'name'}"
        :data="rightsData"
        default-expand-all
        icon-class=" "
        show-checkbox
        node-key="id"
        check-strictly
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogRightVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRights">确 定</el-button>
      </span>
    </el-dialog>

    <Dialog v-if="dialogFormVisible" ref="dialog" v-model="dialogFormVisible" @success="loadRolesList" />
  </div>
</template>

<script>
import { transListToTree } from '@/utils'
import { getPermissions } from '@/api/permission'
import { getCompanyInfo } from '@/api/company'
import { getRolesList, delRoles, getRoleInfo, setRoleRights } from '@/api/roles'
import Dialog from '@/views/setting/components/Dialog'
export default {
  name: 'Setting',
  components: { Dialog },
  data() {
    return {
      activeName: 'first',
      companyInfo: {},
      tableData: [],
      query: {
        page: 1,
        pagesize: 4
      },
      total: 0,
      dialogFormVisible: false,
      dialogRightVisible: false,
      rightsData: [],
      roleID: ''
    }
  },

  created() {
    this.loadCompanyInfo()
    this.loadRolesList()
  },

  methods: {
    async loadCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.$store.getters.companyId)
    },
    async loadRolesList() {
      const res = await getRolesList(this.query)
      this.tableData = res.rows
      this.total = res.total
    },

    handleSizeChange(val) {
      this.query.pagesize = val
      this.loadRolesList()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.loadRolesList()
    },

    async assignPermissions(row) {
      this.roleID = row.id
      this.dialogRightVisible = true
      const res = await getPermissions()
      this.rightsData = transListToTree(res, '0')
      const { permIds } = await getRoleInfo(row.id)
      console.log(permIds)
      this.$refs.treeRights.setCheckedKeys(permIds)
    },

    async saveRights() {
      const permIds = this.$refs.treeRights.getCheckedKeys()
      await setRoleRights(this.roleID, permIds)
      this.dialogRightVisible = false
      this.$message.success('修改成功')
    },
    setRoles(row) {
      this.dialogFormVisible = true
      this.$nextTick(_ => {
        this.$refs.dialog.form = { ...row }
      })
    },
    // 删除角色
    async deleteRoles(row) {
      await delRoles(row.id)
      this.$message.success('删除成功')
      this.loadRolesList()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-tabs__header{
  .el-tabs__item{
    width: 150px;
    text-align: center;
    font-size: 18px;
  }
}
</style>
